<style>
  .customerInfo .el-input.is-disabled .el-input__inner{
    color: black;
  }
  .customerInfo .el-radio__input.is-disabled + span.el-radio__label{
    color: black;
  }
  .customerInfo .el-radio__input.is-disabled.is-checked .el-radio__inner{
    color: black;
  }
  .customerInfo .customerInfoReward .el-form-item__label{
    text-align: left;
  }
  .customerInfo .customerInfoReward .el-form-item{
    margin-bottom: 0;
  }
  .customerInfo .el-upload--picture-card{
    overflow: hidden;
  }
</style>
<style scoped>
  img{
    width:146px;
    height: 146px;
  }
</style>
<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户类型" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择用户类型" clearable size="small">
          <el-option
          v-for="dict in ctmTypeOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
          <el-option
          v-for="dict in ctmStatusOptions"
          :key="dict.dictValue"
          :label="dict.dictLabel"
          :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['customer:info:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="blacklistByNos"
          v-hasPermi="['customer:info:remove']"
        >拉黑</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customer:info:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="infoList" @selection-change="handleSelectionChange" :border="true">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名" align="center" prop="username" />
      <el-table-column label="昵称" align="center" prop="nickname" />
      <el-table-column label="用户类型" align="center" prop="type" :formatter="customTypeFmt" />
      <el-table-column label="身份证号" align="center" prop="idNo" />
      <el-table-column label="手机号" align="center" prop="mobile" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="邮编" align="center" prop="post" />
      <el-table-column label="性别" align="center" prop="sex" :formatter="sexFmt" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="状态" align="center" prop="status" :formatter="customStatusFmt" />
      <el-table-column label="注册日期" align="center" prop="registerDate" width="180">
        <template slot-scope="scope">
          <span>{{ dateFmt(scope.row.registerDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            v-hasPermi="['customer:info:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleReward(scope.row)"
            v-hasPermi="['customer:info:query']"
          >奖励</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="blacklistByNos(scope.row)"
            v-hasPermi="['customer:info:remove']"
          >拉黑</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form class="customerInfo" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <uploadImage v-model="form.avatar"/>
        </el-form-item>
        <el-form-item label="用户类型" prop="type">
          <el-select v-model="form.type" placeholder="请选择用户类型">
            <el-option
            v-for="dict in ctmTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNo">
          <el-input v-model="form.idNo" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="座机" prop="telephone">
          <el-input v-model="form.telephone" placeholder="请输入座机" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="邮编" prop="post">
          <el-input v-model="form.post" placeholder="请输入邮编" />
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-input v-model="form.birthday" placeholder="请输入生日" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择性别">
            <el-option
            v-for="dict in ctmSexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="定位" prop="location">
          <el-input v-model="form.location" placeholder="请输入定位" />
        </el-form-item>
        <el-form-item label="协议编号" prop="agreementNo">
          <customSelect
            v-model="form.agreement"
            :dataList="meanOptions"
            labelKey="meanName"
            valueKey="meanNo"
            v-on:selectPageChange='getMeanOptions'
            v-on:selectChange="updateMeanOptions"
            :pageInfo="queryParams"
            @click="getMeanOptions"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 详情对话框 -->
    <el-dialog :title="title" :visible.sync="openView" width="500px" append-to-body>
      <el-form class="customerInfo" ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="username">
          <el-input disabled v-model="form.username" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickname">
          <el-input disabled v-model="form.nickname" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <div>
            <div class="el-upload--picture-card">
              <div>
                <img :src="form.avatar" alt="">
              </div>
            </div>
          </div>
        </el-form-item>
        <el-form-item label="用户类型" prop="type">
          <el-select disabled v-model="form.type" placeholder="请选择用户类型">
            <el-option
            v-for="dict in ctmTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="身份证号" prop="idNo">
          <el-input disabled v-model="form.idNo" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input disabled v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="座机" prop="telephone">
          <el-input disabled v-model="form.telephone" placeholder="请输入座机" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input disabled v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="邮编" prop="post">
          <el-input disabled v-model="form.post" placeholder="请输入邮编" />
        </el-form-item>
        <el-form-item label="生日" prop="birthday">
          <el-input disabled v-model="form.birthday" placeholder="请输入生日" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select disabled v-model="form.sex" placeholder="请选择性别">
            <el-option
            v-for="dict in ctmSexOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input disabled v-model="form.age" placeholder="请输入年龄" />
        </el-form-item>
        <el-form-item label="定位" prop="location">
          <el-input disabled v-model="form.location" placeholder="请输入定位" />
        </el-form-item>
        <el-form-item label="协议编号" prop="agreementNo">
          <el-input disabled v-model="form.agreementNo" placeholder="请输入协议编号" />
        </el-form-item>
        <el-form-item label="协议版本" prop="agreementVersion">
          <el-input disabled v-model="form.agreementVersion" placeholder="请输入协议版本" />
        </el-form-item>
        <el-form-item label="协议名称" prop="meanName">
          <el-input disabled v-model="form.meanName" placeholder="请输入协议名称" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 奖励对话框 -->
    <el-dialog class="customerInfoReward" :title="title" :visible.sync="openReward" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="用户名:" style="text-align:left;" >{{ form.username }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="注册时间:" >{{ form.registerDate }}</el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="奖励记录" ></el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时间" ></el-form-item>
          </el-col>
          <el-col :span="24">
            <hr>
          </el-col>
        </el-row>
        <el-row v-for="(d,index) in rewardList" :key="index">
          <el-col :span="12">
            {{d.productName}}
          </el-col>
          <el-col :span="12">
            {{d.rewardDate}}
          </el-col>
          <el-col :span="24">
            <hr>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listInfo, getInfo, delInfo, addInfo, updateInfo, exportInfo, blacklistByNos, listInfoVo, getInfoVoByNo } from "@/api/customer/info";
import { getRewardByCustomerNo } from "@/api/customer/reward";
import { listInfoOptions } from "@/api/mean/info";
import UploadImage from '@/components/UploadImage';
import CustomSelect from '@/components/Select';
import { getToken } from "@/utils/auth";

export default {
  name: "Info",
  components: {
    UploadImage,
    CustomSelect
  },
  data() {
    return {
      // 上传的图片服务器地址
      uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/uploadToLongFileSystem",
      headers: {
        toekn: getToken(),
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 选中数组
      nos: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      infoList: [],
      // 奖励表格数据
      rewardList: [],
      // 用户类型集合
      ctmTypeOptions: [],
      // 用户状态集合
      ctmStatusOptions: [],
      // 用户性别集合
      ctmSexOptions: [],
      // 协议集合
      meanOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示弹出层
      openView: false,
      // 是否显示弹出层
      openReward: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        no: null,
        password: null,
        username: null,
        nickname: null,
        avatar: null,
        type: null,
        merchantNo: null,
        idNo: null,
        mobile: null,
        telephone: null,
        address: null,
        post: null,
        birthday: null,
        sex: null,
        age: null,
        location: null,
        status: null,
        agreementNo: null,
        agreementVersion: null,
        registerDate: null,
        registerTime: null,
        modifiedDate: null,
        modifiedTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        username: [
          { required: true, message: "用户名不能为空", trigger: "blur" }
        ],
        nickname: [
          { required: true, message: "昵称不能为空", trigger: "blur" }
        ],
        avatar: [
          { required: true, message: "头像不能为空", trigger: "blur" }
        ],
        type: [
          { required: true, message: "用户类型不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("ctm_customer_type").then(response => {
      this.ctmTypeOptions = response.data;
    });
    this.getDicts("ctm_customer_status").then(response => {
      this.ctmStatusOptions = response.data;
    });
    this.getDicts("sys_user_sex").then(response => {
      this.ctmSexOptions = response.data;
    });
  },
  methods: {
    /** 查询用户列表 */
    getList() {
      this.loading = true;
      listInfoVo(this.queryParams).then(response => {
        this.infoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openView = false;
      this.openReward = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        no: null,
        password: null,
        username: null,
        nickname: null,
        avatar: null,
        type: null,
        merchantNo: null,
        idNo: null,
        mobile: null,
        telephone: null,
        address: null,
        post: null,
        birthday: null,
        sex: null,
        age: null,
        location: null,
        status: "0",
        agreementNo: null,
        agreementVersion: null,
        registerDate: null,
        registerTime: null,
        modifiedDate: null,
        modifiedTime: null,
        agreement: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.nos = selection.map(item => item.no)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const no = row.no
      getInfoVoByNo(no).then(response => {
        this.form = response.data;
        this.getMeanOptions(null,null,null,this.form.agreementNo);
        this.open = true;
        this.title = "修改用户";
      });
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.reset();
      const no = row.no
      getInfoVoByNo(no).then(response => {
        this.form = response.data;
        this.openView = true;
        this.title = "修改用户";
      });
    },
    /** 奖励按钮操作 */
    handleReward(row) {
      this.reset();
      this.form = row;
      this.openReward = true;
      this.title = "用户奖励";
      var query = {};
      query.customerNo = row.no;
      getRewardByCustomerNo(query).then(response => {
        this.rewardList = response.data;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInfo(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInfo(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    blacklistByNos(row) {
      const nos = row.no || this.nos;
      this.$confirm('是否确认拉黑用户编号为"' + nos + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return blacklistByNos(nos);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportInfo(queryParams);
        }).catch(() => {});
    },
    /** 用户类型格式化 */
    customTypeFmt(row, column){
      var val = row.type;
      this.ctmTypeOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 性别格式化 */
    sexFmt(row, column){
      var val = row.sex;
      this.ctmSexOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 用户状态格式化 */
    customStatusFmt(row, column){
      var val = row.status;
      this.ctmStatusOptions.forEach(element => {
        if(val == element.dictValue){
          val = element.dictLabel;
        }
      });
      return val;
    },
    /** 日期格式化 */
    dateFmt(str) {
      return str.substring(0,4)+'-'+str.substring(4,6)+'-'+str.substring(6,8);
    },
    updateMeanOptions(val){
      var noArr = val.split("-");
      var nameArr = noArr[1].split("_");
      var meanNo = noArr[0];
      var meanName = nameArr[0];
      var meanVersion = nameArr[1];
      this.form.agreement = meanName+'_'+meanVersion
      this.form.agreementNo = meanNo;
      this.form.agreementVersion = meanVersion;
      this.form.meanName = meanName;
    },
    getMeanOptions(pageNum,pageSize,queryContent,meanNo){
      if(!pageNum){
        pageNum = this.queryParams.pageSize;
      }
      if(!pageSize){
        pageSize = this.queryParams.pageSize;
      }
      this.queryParams.pageNum = pageNum;
      this.queryParams.pageSize = pageSize;
      this.queryParams.name = queryContent;
      this.queryParams.meanNo = meanNo;

      this.loading = true;
      listInfoOptions(this.queryParams).then(response => {
        this.meanOptions = response.rows;
        if(meanNo){
          this.meanOptions.forEach(element => {
            this.form.agreement = element.meanName+'_'+element.meanVersion
            if(element.meanNo == meanNo){
              this.form.agreementNo = element.meanNo
              this.form.agreementVersion = element.meanName
            }
          });
        }else{
          this.meanOptions.forEach(element => {
            element.meanName = element.meanName+'_'+element.meanVersion
          });
        }
        this.queryParams.total = response.total;
        this.loading = false;
      });
    }
  }
};
</script>
